#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //asetukset
    QSettings settings("./theocopy.ini", QSettings::IniFormat);
    ui->sharename->setText(settings.value("path", "").toString());
    ui->comboBox->setCurrentIndex(settings.value("language","").toInt());
    //ui->comboBox->setHidden(true);
    //ui->label_8->setHidden(true);
    if(!QDBusConnection::systemBus().isConnected()){
        qDebug()<<"Cannot connect to system bus";
    }
    QDBusConnection::systemBus().connect(
                             "org.freedesktop.UDisks",
                             "/org/freedesktop/UDisks",
                             "org.freedesktop.UDisks",
                             "DeviceAdded",
                             this, SLOT(deviceAdded(QDBusObjectPath)));

    ui->progressBar->setVisible(false);
    ui->progressBar_2->setVisible(false);
    ui->progressBar_3->setVisible(false);
    ui->progressBar_4->setVisible(false);
    ui->progressBar_5->setVisible(false);
    ui->progressBar_6->setVisible(false);
    ui->label->setVisible(false);
    ui->label_2->setVisible(false);
    ui->label_3->setVisible(false);
    ui->label_4->setVisible(false);
    ui->label_5->setVisible(false);
    ui->label_6->setVisible(false);
    ui->label_11->setVisible(false);
    ui->label_12->setVisible(false);
    ui->label_13->setVisible(false);
    ui->label_14->setVisible(false);
    ui->label_15->setVisible(false);
    ui->label_16->setVisible(false);
}
void MainWindow::paivat(){

}

QString MainWindow::deviceAdded(QDBusObjectPath dev){

    //QSettings settings("./theocopy.ini", QSettings::IniFormat);
    qDebug() << "device added!"<<dev.path();



     QDBusInterface *device = new QDBusInterface("org.freedesktop.UDisks",dev.path(),"org.freedesktop.UDisks.Device",QDBusConnection::systemBus(),this);
     SleepThread::sleep(2);//5
     QString volumePoint = device->property("DeviceMountPaths").toString();

     if(volumePoint == "") return "";

     paiva1 = ui->paiva1->isChecked();
     paiva2 = ui->paiva2->isChecked();

          luokka *sl = new luokka();
          sl->volumePoint = volumePoint;
          sl->paiva1 = this->paiva1;
          sl->paiva2 = this->paiva2;
          if(!ui->progressBar->isVisible()){
              ui->progressBar->setVisible(true);
              ui->label_11->setVisible(true);
              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );
              ui->label_11->setText(neededWord);
              qDebug() << "progress 1";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged(int,int,bool)));
          }else if(!ui->progressBar_2->isVisible()){

              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );

              ui->label_12->setVisible(true);
              ui->label_12->setText(neededWord);
              ui->progressBar_2->setVisible(true);
              qDebug() << "progress 2";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged2(int,int,bool)));
          }else if(!ui->progressBar_3->isVisible()){

              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );
              ui->label_13->setVisible(true);
              ui->label_13->setText(neededWord);
              ui->progressBar_3->setVisible(true);
              qDebug() << "progress 3";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged3(int,int,bool)));
          }else if(!ui->progressBar_4->isVisible()){

              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );
              ui->label_14->setVisible(true);
              ui->label_14->setText(neededWord);
              ui->progressBar_4->setVisible(true);
              qDebug() << "progress 4";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged4(int,int,bool)));
          }else if(!ui->progressBar_5->isVisible()){

              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );
              ui->label_15->setVisible(true);
              ui->label_15->setText(neededWord);
              ui->progressBar_5->setVisible(true);
              qDebug() << "progress 5";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged5(int,int,bool)));
          }else if(!ui->progressBar_6->isVisible()){

              QStringList pieces = volumePoint.split( "/" );
              QString neededWord = pieces.value( pieces.length() - 1 );
              ui->label_16->setVisible(true);
              ui->label_16->setText(neededWord);
              ui->progressBar_6->setVisible(true);
              qDebug() << "progress 6";
              connect(sl,SIGNAL(filescopied(int,int,bool)),this,SLOT(progressChanged6(int,int,bool)));
          }
          sl->start();

     qDebug() <<"volumepoint "<< volumePoint;

    return volumePoint;
}

MainWindow::~MainWindow()
{
    delete ui;
}


//asetusten tallennus
void MainWindow::on_pushButton_clicked()
{
    QSettings settings("./theocopy.ini", QSettings::IniFormat);

    settings.setValue("path", ui->sharename->text());
    settings.setValue("language",ui->comboBox->currentIndex());

}

void MainWindow::progressChanged(int max, int value, bool ok)
{
    if(max == 0){
        ui->label->setVisible(true);
        ui->label->setText(tr("Files not found"));
    }else if(!ok){
        ui->label->setVisible(true);
        ui->label->setText(tr("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label->setHidden(true);
    ui->progressBar->setVisible(value != max);
    ui->progressBar->setMaximum(max);
    ui->progressBar->setValue(value);
    }
    if(value == max){
        ui->label->setVisible(true);
        ui->label->setText(tr("Copying is ready!"));
    }
}
void MainWindow::progressChanged2(int max, int value, bool ok)
{
    if(max == 0){
        ui->label_2->setVisible(true);
        ui->label_2->setText(tr("Files not found"));
        return;
    }else if(!ok){
        ui->label_2->setVisible(true);
        ui->label_2->setText(tr("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label_2->setHidden(true);
    ui->progressBar_2->setVisible(value != max);
    ui->progressBar_2->setMaximum(max);
    ui->progressBar_2->setValue(value);
    }
    if(value == max){
        ui->label_2->setVisible(true);
        ui->label_2->setText(tr("Copying is ready!"));
    }
}
void MainWindow::progressChanged3(int max, int value, bool ok)
{
    if(max == 0){
        ui->label_3->setVisible(true);
        ui->label_3->setText(tr("Files not found"));
    }else if(!ok){
        ui->label_3->setVisible(true);
        ui->label_3->setText(tr("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label_3->setHidden(true);
    ui->progressBar_3->setVisible(value != max);
    ui->progressBar_3->setMaximum(max);
    ui->progressBar_3->setValue(value);
    }
    if(value == max){
        ui->label_3->setVisible(true);
        ui->label_3->setText(tr("Copying is ready!"));
    }
}
void MainWindow::progressChanged4(int max, int value, bool ok)
{
    if(max == 0){
        ui->label_4->setVisible(true);
        ui->label_4->setText(tr("Files not found"));
    }else if(!ok){
        ui->label_4->setVisible(true);
        ui->label_4->setText(("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label_4->setHidden(true);
    ui->progressBar_4->setVisible(value != max);
    ui->progressBar_4->setMaximum(max);
    ui->progressBar_4->setValue(value);
    }
    if(value == max){
        ui->label_4->setVisible(true);
        ui->label_4->setText(tr("Copying is ready!"));
    }
}
void MainWindow::progressChanged5(int max, int value, bool ok)
{
    if(max == 0){
        ui->label_5->setVisible(true);
        ui->label_5->setText(tr("Files not found"));
    }else if(!ok){
        ui->label_5->setVisible(true);
        ui->label_5->setText(tr("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label_5->setHidden(true);
    ui->progressBar_5->setVisible(value != max);
    ui->progressBar_5->setMaximum(max);
    ui->progressBar_5->setValue(value);
    }
    if(value == max){
        ui->label_5->setVisible(true);
        ui->label_5->setText(tr("Copying is ready!"));
    }
}
void MainWindow::progressChanged6(int max, int value, bool ok)
{
    if(max == 0){
        ui->label_6->setVisible(true);
        ui->label_6->setText(tr("Files not found"));
    }else if(!ok){
        ui->label_6->setVisible(true);
        ui->label_6->setText(tr("There is no space"));
        //QMessageBox::warning(this,"","Levyllä ei tilaa!");
        return;
    }else{
    ui->label_6->setHidden(true);
    ui->progressBar_6->setVisible(value != max);
    ui->progressBar_6->setMaximum(max);
    ui->progressBar_6->setValue(value);
    }
    if(value == max){
        ui->label_6->setVisible(true);
        ui->label_6->setText(tr("Copying is ready!"));
    }
}
void MainWindow::on_actionExit_triggered()
{
    close();
}

void MainWindow::on_actionAbout_triggered()
{
    QString tietoja;

    tietoja.append("TheoCopy version 1.4");
    tietoja.append("\n");
    tietoja.append("\n http://www.TheocBase.net");
    QMessageBox::about(this,"TheoCopy",tietoja);
}
